package com.bsj.travel.process.handle.service.impl;

import com.alibaba.fastjson2.JSON;
import com.bsj.consumerQ.function.HandleRun;
import com.bsj.travel.config.rocketmq.producer.BaseMqPushUtil;
import com.bsj.travel.constant.BaseServerTopicConstants;
import com.bsj.travel.def.common.DTO.mqtt.MqttEvent;
import com.bsj.travel.def.common.VO.device.DeviceInfoVO;
import com.bsj.travel.def.global.mq.Rp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 自定义主题通信处理实现类[设备上行]
 * @date 2024/1/16
 */
@Service
@Slf4j
public class CustomTopicMessageHandleRun implements HandleRun {

    @Autowired
    private BaseMqPushUtil baseMqPushUtil;

    @Override
    public void doRun(List<byte[]> list) {
        for (byte[] bytes : list) {
            String message = new String(bytes);
            MqttEvent event = JSON.parseObject(message, MqttEvent.class);
            String client = event.getClient();
            String[] split = client.split("&");
            String productKey = split[1];
            String deviceName = split[0];
            //设备自定义主题消息上行通知
            DeviceInfoVO vo = new DeviceInfoVO();
            vo.setDeviceName(deviceName);
            vo.setData(JSON.toJSONString(event));
            Rp<DeviceInfoVO> rp = new Rp<>();
            rp.setProductKey(productKey);
            rp.setTag(BaseServerTopicConstants.DEVICE_REPORT_INFO_TAG);
            rp.setData(vo);
            baseMqPushUtil.sendTopicBytes(BaseServerTopicConstants.PRODUCT_SUBSCRIBE_TOPIC,
                    BaseServerTopicConstants.DEVICE_REPORT_INFO_TAG, JSON.toJSONBytes(rp));
        }
    }
}
